package com.zkny.zknyerp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkny.zknyerp.mapper.ScenarioMapper;
import com.zkny.zknyerp.model.Scenario;
import com.zkny.zknyerp.service.ScenarioService;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 用户服务接口实现类
 */
@Service
@Transactional
public class ScenarioServiceImpl implements ScenarioService {

    @Autowired
    private ScenarioMapper scenarioMapper;

    /**
     * 设备场景添加接口
     * @param scenario
     * @return
     */
    public Result addScenario(String scenario){
        Example example = new Example(Scenario.class);
        example.createCriteria().andEqualTo("scenario",scenario);
        Scenario scenario1 = scenarioMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(scenario1)){
            return new Result(Constant.code_1017,Constant.msg_1017);
        }

        Scenario scenario2 = new Scenario();
        scenario2.setScenario(scenario);
        int i = scenarioMapper.insert(scenario2);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 场景查询接口
     */
    public Result selectScenario(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Scenario> list = scenarioMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        PageInfo<Scenario> pageInfo = new PageInfo<Scenario>(list);
        return new Result(Constant.code_0000,Constant.msg_0000,pageInfo);
    }

    /**
     * 输入框选项查询
     */

    public Result getScenario(){
        List<Scenario> list = scenarioMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,list);
    }

    /**
     * 场景数据修改接口
     */
    public Result updateScenario(Scenario scenario){
        int i = scenarioMapper.updateByPrimaryKeySelective(scenario);
        if(0 == i ){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }


    /**
     * 场景删除接口
     */
    public Result deleteScenario(Scenario scenario){
        int i = scenarioMapper.deleteByPrimaryKey(scenario);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 根据id查询数据
     */
    public Result getId(Integer id){
        Example example = new Example(Scenario.class);
        example.createCriteria().andEqualTo("id",id);
        Scenario scenario = scenarioMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(scenario)){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,scenario);
    }

}
